
import { _decorator, Component, Node, v3, Vec3, sp } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import GlobalPool from '../../Script/Common/GlobalPool';
import GameConfig from '../../Script/GameSpecial/GameConfig';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import { BaseTransform } from './BaseTransform';
import MapData from '../../Script/GameSpecial/MapData';
import GlobalData from '../../Script/Common/GlobalData';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
const { ccclass, property } = _decorator;

/**障碍物组 */
@ccclass('BarrierGroup')
export class BarrierGroup extends BaseTransform {

    public onColl(role) {
        this.canColl = false;
        let z = GlobalData.get(GlobalEnum.myGlobalDataType.player).node.position.z;
        //玩家视野前方的障碍物，播放撞毁动画
        if (this.node.position.z - 10 < z && this.node.position.z + 80 > z) {
            let x = 0;//todo:车辆X坐标，用于判断障碍物向左还是向右飞出
            let children = this.node.children;
            let p = v3();
            let s = v3();
            let arr = [];
            for (let i = children.length - 1; i >= 0; --i) {
                let node = children[i];
                this.getChildWorldPos(node, p);
                let r = Math.random() * 60 + 10;
                if (Math.random() < 0.5) {
                    r *= 0.017453;
                } else {
                    r *= -0.017453;
                }
                let speedZ = Math.random() * 20 + 20;
                s.x = -speedZ * Math.sin(r);
                s.z = -speedZ * Math.cos(r);
                s.y = 20 + Math.random() * 10;
                let barrier = GlobalPool.get(node.name, { p: p, s: s });
                arr.push(barrier);
            }
            this.emit(EventType.LevelEvent.addBarriers, arr);
        }
        this.emit(EventType.LevelEvent.removeBarrierGroup, this);
    }
    protected getChildWorldPos(node: Node, p: Vec3) {
        node.getPosition(p);
        p.z = this.z;
        return p;
    }


    /************************************************************管理属性************************************************************/

}
